package com.iraeta.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.entity.Iraorder;
import com.iraeta.entity.PermissionType;
import com.iraeta.entity.resulttype.IraorderResultType;
import com.iraeta.service.iraorder.IraorderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * 内部用户维护订单功能
 * */
@RestController(value = "IraOrderController")
@RequestMapping("emp/order")
public class IraOrderController extends BaseController{
    @Resource
    IraorderService iraorderService;

    @RequestMapping("pageorder")
    public String pageOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String title = jo.getString("title");
        this.pageSet(jo);
        List<IraorderResultType> IraorderList = iraorderService.listIraorderByUserId(this.getUserId(), orderno, title);
        PageInfo<IraorderResultType> IraorderPageInfo = this.getPageInfo(IraorderList);
        return JSON.toJSONString(Result.success(IraorderPageInfo));
    }

    @RequestMapping("getorder")
    public String getOrder(@RequestBody JSONObject paramJb){
        int orderId = paramJb.getIntValue("id");
        Iraorder iraorder = iraorderService.getIraorder(orderId);
        return this.success(iraorder);
    }

    @RequestMapping("checkorder")
    public String checkOrder(@RequestBody JSONObject paramJb){
        int orderId = paramJb.getIntValue("id");
        if(this.getPermissions().contains(PermissionType.yuanGong)){
            return this.success();
        }
        if(this.getPermissions().contains(PermissionType.wlUser)){
            return this.error();
        }
        if(this.getPermissions().contains(PermissionType.keHu)){
            Iraorder iraorder = this.iraorderService.getIraorder(orderId);
            if(iraorder == null){
                return this.error();
            }
            if(iraorder.getKehuuserid() == this.getUserId()){
                return this.success();
            }
        }
        return this.error();
    }

    @RequestMapping("getordermap")
    public String getOrderMap(@RequestBody JSONObject paramJb){
        int orderId = paramJb.getIntValue("id");
        IraorderResultType iraorder = iraorderService.getIraorderResultType(orderId);
        return this.success(iraorder);
    }

    @RequestMapping("addeditorder")
    public String addEditOrder(@RequestBody JSONObject paramJb){
        int orderId = paramJb.getIntValue("id");
        Iraorder iraorder = orderId == 0 ? new Iraorder() : iraorderService.getIraorder(orderId);
        this.mapJsonToObj(paramJb, iraorder);
        JSONArray productJSONArray = paramJb.getJSONArray("productList");
        JSONArray scStepJSONArray = paramJb.getJSONArray("scStepList");
        if(orderId == 0){
            iraorder.setCreateuserid(this.getUserId());
            iraorderService.addIraorder(iraorder, productJSONArray, scStepJSONArray);
        }else {
            iraorderService.updateIraorder(iraorder, productJSONArray, scStepJSONArray);
        }
        return this.success();
    }

    @RequestMapping("deleteorder")
    public String deleteOrder(@RequestBody JSONObject paramJb){
        int orderId = paramJb.getIntValue("id");
        iraorderService.deleteIraorder(orderId);
        return this.success();
    }

    /*客户查看自己关联的订单*/
    @RequestMapping("pagekehuorder")
    public String pageKeHuOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String title = jo.getString("title");
        this.pageSet(jo);
        List<IraorderResultType> IraorderList = iraorderService.listIraorderByKeHuId(this.getUserId(), orderno, title);
        PageInfo<IraorderResultType> IraorderPageInfo = this.getPageInfo(IraorderList);
        return JSON.toJSONString(Result.success(IraorderPageInfo));
    }
}
